package com.jfree.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.jfree.entity.general.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;

public class JwtUtil {

    public static final Long DEFAULTTIME = 5 * 60 * 1000L;

    public static String getTJwtToken(User user) {
        Predicate<String> fg = (n) -> n.startsWith("J");
        String token = "";
        token = JWT.create().withAudience(String.valueOf(user.getId())).sign(Algorithm.HMAC256(user.getPassword()));
        return token;
    }

    public static String getJwtToken(User user) {
        try {
            Date date = new Date(System.currentTimeMillis() + DEFAULTTIME);

            Algorithm algorithm = Algorithm.HMAC256(user.getPassword());
            return JWT.create().withClaim("sex", "12").withExpiresAt(date).withIssuer("gh").sign(algorithm);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main1(String[] args) throws InterruptedException {
        User u = new User();
        u.setId(12L);
        u.setName("t1");
        u.setSex(1);
        u.setPassword("123");
        Algorithm algorithm = Algorithm.HMAC256(u.getPassword());
        JWTVerifier verifier = JWT.require(algorithm).build();
        DecodedJWT dt = verifier.verify(getJwtToken(u)); //JWTVerificationException
        System.out.println(" dt.getIssuer()=" + dt.getIssuer());

        System.out.println(dt.getType());
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("g");
        list.add("k");
        list.add("k");
        list.add("df");
        list.stream().distinct().forEach(System.out::println);
    }
}
